<?php
include("mainfile.php");

$page = new Page;

$id = $_GET['id']*1;
echo $page->printPage($id);

class Page {

    // page data
    var $id = 0; // ID of the selected page
	var $dir      = 0; // directory ID of the selected page
	var $name = "";
	var $language = "";
	var $created = "";
	var $title = "";
	var $keywords = "";
	var $content = "";
	
	
    function printPage($id = 1) {
	    global $tpl;
	    
		$this->getPageData($id);
		
        // get the template
        $tpl->getTpl("print.tpl");
        $tpl->replaceCommonTags();
        $tpl->replaceTags(array(
        TITLE       =>  $this->title,
        KEYWORDS    =>  $this->keywords,
        CONTENT     =>  $tpl->parse($this->content),
		PRINTINFO   =>  $this->printInfo(),
        ));
        
		return $tpl->page;
	}

    function getPageData($id = 1) {
        global $table,$db,$lang;
	    
		if ($id == 0 || !$this->validPage($id, $lang->current)) { $id = $this->getSmallestId($lang->current); }
		
    	$result = $db->query("SELECT * FROM $table->page WHERE id='$id' AND language='$lang->current'");
	    if ($db->numRows($result) == 0)
    	{
	        $this->content = _NOCONTENTAVAILABLE;
    	}
	    else
    	{
	        $row = $db->nextRecord($result);
			$this->id       = $row->id;
			$this->dir      = $row->dir;
			$this->language = $row->language;
	        $this->title    = $row->title;
    		$this->keywords = $row->keywords;
	    	$this->content  = $row->content;
	    }
    }


	function validPage ($id, $language) {
	    global $table,$db;
		
		if ($db->numRows($db->query("SELECT * FROM $table->page WHERE id='$id' AND language='$language'")) == 0)
		    { return false; }
		else
		{
		    return true;
		}
	}


    function getSmallestId ($language, $dir = 0) {
	    global $table,$db;
		
		$query = "SELECT * FROM $table->page WHERE language='$language'";
		if ($dir>0) { $query.= " AND dir='$dir'"; }
		$query.= " ORDER BY id LIMIT 0,1";
		$result = $db->query($query);
		if ($db->numRows($result) == 0)
		{
		    $id = 0;
		}
		else
		{
		    $row = $db->nextRecord($result);
			$id  = $row->id;
		}
		
		return $id;
	}
	
	
	function getRoute($id = 0) {
	    global $table,$db,$admin;
	    
	    $str = "";
	    $result = $db->query("SELECT * FROM $table->dir WHERE id='$id' AND language='$this->language'");
	    if ($db->numRows($result) > 0)
        {
	    	$row = $db->nextRecord($result);
	    	$str.= $this->getRoute($row->parent)."<a href='".GLOBAL_SELF."?op=d&id=$row->id'><b>".$row->name."</b></a>/";
	    }
	    
	    return $str;
	}


    function printInfo() {
	    $str = "<b>www.dankezu.de</b> | "._CREATED.": ".date("d.m.Y H:i:s");
		return $str;
	}

}
?>